import { makeAutoObservable } from 'mobx';
import { Agent, ModelOption, VectorKnowledge, ReferenceFunction } from './agentTypes';
import { getVectorList, getFunctionList, getAgentList, createAgent, updateAgent, deleteAgent, getInfo, getActiveModel } from '../../../../apis/backhub/agent';

class AgentStore {
  agents: Agent[] = [];
  modalVisible = false; // 弹窗显示状态
  editingAgent: Agent | undefined = undefined; // 当前编辑的智能体，undefined表示新建
  loading = false;
  modelValues: any;
  vectorValues: any;
  functionValues: any;

  modelOptions: ModelOption[] = [];
  vectorOptions: VectorKnowledge[] = [];
  functionOptions: ReferenceFunction[] = [];

  constructor() {
    makeAutoObservable(this);
  }

  // 获取所有选项数据
  fetchOptions = async () => {
    try {
      const response = await getActiveModel();
      this.modelValues=[];
      this.modelOptions=[];
      this.modelValues = (response as any)?.data;
      this.modelValues && Object.entries(this.modelValues).map(([roleId, config]) => (
        this.modelOptions.push(config as any)
      ));
    } catch (error) {
      console.error('获取智能体列表失败:', error);
    } finally {
      this.loading = false;
    }

    try {
      const response = await getVectorList();
      this.vectorValues=[];
      this.vectorOptions=[];
      this.vectorValues = (response as any)?.data;
      this.vectorValues && Object.entries(this.vectorValues).map(([roleId, config]) => (
        this.vectorOptions.push(config as any)
      ));
    } catch (error) {
      console.error('获取智能体列表失败:', error);
    } finally {
      this.loading = false;
    }

    try {
      const response = await getFunctionList();
      this.functionValues=[];
      this.functionOptions=[];
      this.functionValues = (response as any)?.data;
      this.functionValues && Object.entries(this.functionValues).map(([roleId, config]) => (
        this.functionOptions.push(config as any)
      ));
    } catch (error) {
      console.error('获取智能体列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 获取智能体列表
  fetchAgents = async () => {
    this.loading = true;
    try {
      const response = await getAgentList();
      this.agents = (response as any)?.data;
    } catch (error) {
      console.error('获取智能体列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 显示创建弹窗
  showCreateModal = () => {
    this.editingAgent = undefined;
    this.modalVisible = true;
  };

  // 显示编辑弹窗
  showEditModal = (agent: Agent) => {
    this.getAgent(agent.id);
    this.modalVisible = true;
  };

  // 隐藏弹窗
  hideModal = () => {
    this.modalVisible = false;
    this.editingAgent = undefined;
  };

  //获取模型
  getAgent = async (id: string) => {
    try {
      this.loading = true;
      const response = await getInfo(id);
      this.editingAgent = (response as any).data;
      return this.editingAgent; // 返回获取到的用户数据
    } catch (error) {
      console.error('获取用户失败:', error);
      throw error;
    } finally {
      this.loading = false;
    }
  };

  // 保存智能体
  saveAgent = async (values: Partial<Agent>) => {
    try {
      values.isStreaming = values?.isStreamingBool ? '1' : '0';
      if (this.editingAgent && this.editingAgent.id) {
        await updateAgent(this.editingAgent.id, values);
      } else {
        await createAgent(values);
      }
      this.hideModal();
      this.fetchAgents();
    } catch (error) {
      console.error('保存智能体失败:', error);
    }
  };

  // 删除智能体
  deleteAgent = async (id: string) => {
    try {
      await deleteAgent(id);
      this.fetchAgents();
    } catch (error) {
      console.error('删除智能体失败:', error);
    }
  };
}

export const agentStore = new AgentStore(); 